package com.booksmanager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booksmanager.annotation.Log;
import com.booksmanager.pojo.Role;
import com.booksmanager.pojo.User;
import com.booksmanager.service.RoleService;
import com.booksmanager.utils.R;
import com.booksmanager.utils.RequestSession;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Yan
 * @since 2020-05-24
 */
@Api(value = "/role", tags = "角色表")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色
     * @param page
     * @param role
     * @return
     */
    @ApiOperation(value = "分页查询角色")
    @GetMapping("/page")
    public R page(Page page, Role role) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag",0);
        return new R(roleService.page(page,wrapper));
    }

    /**
     * 更具id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "更具id查询")
    @GetMapping("/queryById")
    public R queryById(String id) {
        return new R(roleService.getById(id));
    }

    /**
     * 增加role
     * @param role
     * @return
     */
    @ApiOperation(value = "增加role")
    @Log("增加role")
    @PostMapping("/save")
    public R save(@RequestBody Role role) {
        RequestSession requestSession = new RequestSession();
        User user = requestSession.getUser();
        role.setCreateId(user.getId());
        role.setCreateName(user.getUsername());
        return new R(roleService.save(role));
    }

    /**
     * 更新role
     * @param role
     * @return
     */
    @ApiOperation(value = "角色更新")
    @Log("角色更新")
    @PutMapping("/update")
    public R update(@RequestBody Role role) {
        return new R(roleService.updateById(role));
    }

    /**
     * 删除role
     * @param role
     * @return
     */
    @ApiOperation(value = "删除角色")
    @Log("删除角色")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Role role) {
        role.setDelFlag("-1");
        return new R(roleService.updateById(role));
    }

}

